#!/bin/bash
APP=online_edu

if [ -n "$2" ] ;then
   do_date=$2
else
   echo "请传入日期参数"
   exit
fi

dws_traffic_user_video_finish_1d="
insert overwrite table ${APP}.dws_traffic_user_video_finish_1d partition (dt)
select user_id,
       t1.course_id,
       t1.chapter_id,
       video_id,
       position_sec,
       position_sec / during_sec * 100                                    play_sec_rate,
       if((position_sec / during_sec) * 100 >= 90, date_id, '9999-12-31') finish_data,
       if((position_sec / during_sec) * 100 >= 90, date_id, '9999-12-31')
from (
         select user_id,
                course_id,
                chapter_id,
                video_id,
                max(position_sec) position_sec,
                sum(play_sec)     play_sec,
                max(date_id)      date_id
         from ${APP}.dwd_traffic_video_play_inc
         group by user_id, course_id, chapter_id, video_id
     ) t1
         left join ${APP}.dim_video_full t2
                   on t1.video_id = t2.id;
"

dws_traffic_session_page_view_1d="
insert overwrite table ${APP}.dws_traffic_session_page_view_1d partition(dt='$do_date')
select
    session_id,
    mid_id,
    brand,
    model,
    operate_system,
    version_code,
    channel,
    sum(during_time),
    count(*)
from ${APP}.dwd_traffic_page_view_inc
where dt='$do_date'
group by session_id,mid_id,brand,model,operate_system,version_code,channel;
"


case $1 in
    "dws_traffic_user_video_finish_1d" )
        hive -e "$dws_traffic_user_video_finish_1d"
   ;;
    "dws_traffic_session_page_view_1d" )
        hive -e "$dws_traffic_session_page_view_1d"
   ;;

   "all" )
         hive -e "$dws_traffic_user_video_finish_1d$dws_traffic_session_page_view_1d"
   ;;
esac
